package com.shop.shoppingmall.util;

import java.util.HashMap;

public class JSONObject extends HashMap<String, Object>
{
	public static JSONObject fromString(String jsonStr){
		try{
			return Jackson.toObj(jsonStr, JSONObject.class);
		}
		catch(Exception e){
			return null;
		}
	}

	public int getInt(String key)
	{
		try{
			return (int)this.get(key);
		}
		catch(ClassCastException ex){
			return Integer.parseInt(this.get(key).toString());
		}
	}
	public Double getDouble(String key){
		try {
			return (Double) this.get(key);
		}catch (ClassCastException e){
			return Double.valueOf(this.get(key).toString());
		}
	}
	public float getFloat(String key){
		try {
			return (float) this.get(key);
		}catch (ClassCastException e){
			return Float.valueOf(this.get(key).toString());
		}
	}
	public HashMap<String, Object> getJSONObject(String key){
//		ObjectMapper objectMapper = new ObjectMapper();
//		objectMapper.readT
			return (HashMap<String, Object>) this.get(key);
	}

	public String getString(String key){
		try{
			return (String)this.get(key);
		}
		catch(ClassCastException ex){
			return this.get(key).toString();
		}
	}

	public Boolean getBoolean(String key){
		try{
			return (Boolean)this.get(key);
		}
		catch(ClassCastException ex){
			return Boolean.parseBoolean(this.get(key).toString());
		}
	}

	/*public String toString() {
		return Jackson.toJson(this);
	}*/

	public JSONObject(String key, Object value) {
		this.put(key,  value);
	}

	public JSONObject() {
	}
}
